//
//  RCalendarViewController.m
//  Riji
//
//  Created by liujianlong on 2022/8/18.
//

#import "RCalendarViewController.h"
#import "RCalendarCell.h"
#import "RCalendarLayout.h"

@interface RCalendarViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *dateLbl;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, assign) int stamp;
@property (nonatomic, strong) NSDateComponents *date;
@property (nonatomic, strong) NSDateComponents *maxDate;
@property (nonatomic, assign) int num;
@property (nonatomic, assign) CGFloat oldOffsetx;
@end

@implementation RCalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.title = LS(@"日历");
    self.num = 1;
    self.oldOffsetx = 0;
    self.stamp = [[NSDate date] timeIntervalSince1970];
    self.maxDate = self.date;
    
    self.navigationItem.leftBarButtonItem = nil;
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, StuBarHight + 5, 40, 40)];
    [rightBtn setTitle:LS(@"今天") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(actionForRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *baritem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = baritem;
    
    RCalendarLayout *layout = [[RCalendarLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsZero;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, self.collectionView.frame.size.height);
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.scrollEnabled = NO;
    [self.collectionView registerNib:[RCalendarCell nib] forCellWithReuseIdentifier:[RCalendarCell ID]];
    
}
- (NSDateComponents *)date{
    return [NSDate dateOfStamp:self.stamp];
}
- (void)setStamp:(int)stamp{
    _stamp = stamp;
    NSDateFormatter *formatter  = [[NSDateFormatter alloc] init];
    formatter.dateFormat = LS(@"yyyy年/MM月");
    self.dateLbl.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.stamp]];
    [self.collectionView reloadData];
}
#pragma mark - 回到今天
- (void)actionForRightBtn:(UIButton *)sender{
    self.stamp = [[NSDate date] timeIntervalSince1970];
}
#pragma mark - 左右切换日期
- (IBAction)actionForChange:(UIButton *)sender {
    int year = sender.tag == 201 ? (int)self.date.year - 1 : (int)self.date.year + 1;
    //    if (year > self.maxDate.year) { return;}
    NSString *string = [NSString stringWithFormat:LS(@"%d年/%d月"),year,(int)self.date.month];
    self.stamp = [[NSDate tranToStamp:string :LS(@"yyyy年/MM月")] intValue];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x < 0) {
        self.stamp = self.stamp - 86400 * 30;
    }else if (translation.x > 0) {
        self.stamp = self.stamp + 86400 * 30;
    }
    return NO;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.num;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RCalendarCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RCalendarCell ID] forIndexPath:indexPath];
    cell.stamp = self.stamp;
    if (cell.gestureRecognizers.count <= 0) {
        UIPanGestureRecognizer *pan = [UIPanGestureRecognizer new];
        pan.delegate = self;
        [cell addGestureRecognizer:pan];
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.oldOffsetx > scrollView.contentOffset.x) {
        self.stamp = self.stamp + 86400 * 30;
    }else if (self.oldOffsetx < scrollView.contentOffset.x){
        self.num += 1;
        self.stamp = self.stamp - 86400 * 30;
    }
    self.oldOffsetx = scrollView.contentOffset.x;
}

#pragma mark - UICollectionViewDelegateFlowLayout
@end
